<?php
require_once '_inc.php';
$linkID = intval( $_GET['sId'] );
$op     = $_GET['tp'];

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	$id = intval($_POST['id']);
	$sitename = $_POST['sitename'];
	$sitelink = $_POST['sitelink'];
	$sort = intval($_POST['sort']);
	if( $id ) {
		$sql = "update ".$gDb['prefix']."link set sitename='$sitename', sitelink='$sitelink', sort='$sort' where id=$id";
		$q   = $db->query($sql);
	} else {
		$sql = "insert into ".$gDb['prefix']."link (`sitename`,`sitelink`,`sort`)values('$sitename','$sitelink','$sort')";
		$q   = $db->query($sql);
	}
	$backUrl = $_POST['backUrl'];
	if ( !empty( $backUrl ) )
	{
		header( "location: ".base64_decode( $backUrl ) );
		exit( );
	}
	header( "location: site_link.php");
	exit( );	
}

switch ($op)
{
	case 'add':
		$tpl->display('linkedit.tpl');
		break;
	case 'edit':
		$link = $db->fetchrow("select * from ".$gDb['prefix']."link where id=$linkID");
		$tpl->assign('link',$link);
		$tpl->display('linkedit.tpl');
		break;
	case 'del':
		{
			if($linkID)
			{
			$sql = "delete from ".$gDb['prefix']."link where id=$linkID limit 1";
			if ( $db->query($sql) )
			{
				$backUrl = $_POST['backUrl'];
				if ( !empty( $backUrl ) )
				{
					header( "location: ".base64_decode( $backUrl ) );
					exit( );
				}
			}
			header( "location: site_link.php");
			exit( );
			} else {
				echo 'link id 不能为空';
			}
		}
		break;
}